<?php

class AjaxResponder extends Func_Controller{
	
	protected $response;
	
	protected $placeHolderValues;
	
	/**
	 * AjaxResponder gets the html response from the provided template.
	 * Any placeholder replacements are also performed
	 * 
	 * @param a smarty tpl template file $responseTemplate
	 * @param associative array of placeholder names and values $phv
	 */
	public function AjaxResponder($responseTemplate, array $phv){
		
		$this->response = "ajax";
		$this->placeHolderValues = $phv;
			
		Validate::notNull($responseTemplate, "You must specify a response template to use.");
		$this->view = $responseTemplate;
		
		parent::__construct();
		
		/*
		 * exit is important as it stops the application from continuing
		 * and including the complete page template in the response
		 * html
		 */
		exit;
	}
	
	protected $view;
	
	protected function render(){

		/*
		 * if there are no placeholder values then return
		 */
		if($this->placeHolderValues == null || count($this->placeHolderValues) < 1){
			return;
		}
		
		if(!isset($this->view)){
			throw new Exception("You have not specified the html template to use
				for this response");
		}
		
		foreach($this->placeHolderValues as $v){
			$keys = array_keys($this->placeHolderValues, $v);
			$this->value($keys[0], $v);
		}
	}
	
}
?>